

//
//  UiViewExtension.swift
//  YouCaiShoppingMall
//
//  Created by 优裁科技 on 2017/9/21.
//  Copyright © 2017年 优裁科技. All rights reserved.
//

import UIKit

/**
 * UIView 扩展
 */
extension UIView {
    
    //MARK: - 设置带文本的角标
    /**
     * 设置带文本的角标
     */
    func makeBadgeText(text:String,
                       textColor tColor:UIColor = UIColor.white,
                       txtFont tFont:UIFont = UIFont.systemFont(ofSize: 6),
                       backColor bColor:UIColor = UIColor.init().colorFromHexInt(hex: 0xfd7e18)){
        
        var rect:CGRect?
        let textSize:CGSize = NSString.init(string: text).boundingRect(with: CGSize.init(width: self.frame.size.width, height: CGFloat.greatestFiniteMagnitude),
                                                                       options: .usesLineFragmentOrigin,
                                                                       attributes: [NSAttributedString.Key.font:tFont],
                                                                       context: nil).size
        
        //UIButton
        if self.isKind(of: UIButton.classForCoder()) {
            unowned let weakButton:UIButton = self as! UIButton
            rect = CGRect.init(x: (weakButton.titleLabel?.frame.size.width)! + (weakButton.titleLabel?.frame.origin.x)!,
                               y: (weakButton.titleLabel?.frame.origin.y)!,
                               width: textSize.width + 8,
                               height: textSize.height)
        }
        else{
            rect = CGRect.init(x: self.frame.size.width - (textSize.width + 8) * 0.5,
                               y: -textSize.height * 0.5,
                               width: textSize.width + 8,
                               height: textSize.height)
        }
        
        let _txt:String = Int.init(text)! > 99 ? "…" : text
        self.badgeLable().makeBrdgeViewWithText(text: _txt, textColor: tColor, backColor: bColor, textFont: tFont, tframe: rect!)
    }
    
    //MARK: - 设置没有文本的角标
    /**
     * 设置没有文本的角标
     */
    func makeBadge(corner:CGFloat? = 3,
                   cornerColor color:UIColor? = UIColor.init().colorFromHexInt(hex: 0xfd7e18)){
        
        let labBadge:YeeBadgeLable = self.badgeLable()
        labBadge.frame = CGRect.init(x: self.frame.size.width - corner!, y: -0.3 * corner!, width: corner! * 2, height: corner! * 2)
        labBadge.makeBrdgeViewWithCor(corner: corner!, CornerColor: color!)
    }
    
    //MARK: - 移除角标
    /**
     * 移除角标
     */
    func removeBadgeView() {
        self.badgeLable().removeFromSuperview()
    }
    
    /////////////////////////////////////////////////////////
    
    /** badgeLable  */
    private func badgeLable() -> YeeBadgeLable {
        
        var badgeLable:YeeBadgeLable? = self.viewWithTag(99999) as? YeeBadgeLable
        if badgeLable == nil {
            badgeLable = YeeBadgeLable.init()
            badgeLable?.tag = 99999
            
            self.addSubview(badgeLable!)
        }
        return badgeLable!
    }
    
    
    //MARK: - 设置圆角
    /// 设置视图圆角
    /// - Parameter corners: 圆角的方位(UIRectCorner.topRight,UIRectCorner.topLeft,...)
    /// - Parameter w: 边框宽度
    /// - Parameter bc: 变宽颜色
    /// - Parameter fc: 填充颜色
    /// - Parameter r: 圆角半径
    func setCornerRadiusFor(UIRectCorner corners:UIRectCorner,
                            borderWidth w:CGFloat,
                            borderColor bc:UIColor = UIColor.clear,
                            fillColor fc:UIColor = UIColor.clear,
                            andCorneradius r:CGFloat) -> CAShapeLayer {
        
        let path = UIBezierPath.init(roundedRect: self.bounds,
                                     byRoundingCorners: corners,
                                     cornerRadii: CGSize.init(width: r, height: r))
        
        let layer = CAShapeLayer.init()
        layer.path = path.cgPath
        
        layer.lineWidth = w
        layer.lineCap = CAShapeLayerLineCap.square
        layer.strokeColor = bc.cgColor
        
        //  注意直接填充layer的颜色，不需要设置控件view的backgroundColor
        layer.fillColor = fc.cgColor
        return layer
    }
    
    
    /// 获取触发事件在视图中的位置
    func zhmfPositionInScreen() -> CGPoint {
        /// 先判断是否有父视图，如果没有父视图，直接返回视图的位置就行
        if let superView = self.superview {
            /**
             判断父视图是否是UIScrollView或者继承自UIScrollView
             
             先使用视图在屏幕上的位置使用视图的位置与父视图的位置X与Y分别相加
             
             如果父视图不是UIScrollView并且不继承自UIScrollView，则直接返回结果
             
             如果父视图是UIScrollView或者继承自UIScrollView
             
             还需要分别减去UIScrollView的scrollViewOffset.x和scrollViewOffset.y，然后返回结果
             */
            if let scrollView = superView as? UIScrollView {
                let position = CGPoint.init(x: self.frame.origin.x, y: self.frame.origin.y)
                let superPosition = superView.zhmfPositionInScreen()
                let scrollViewOffset = scrollView.contentOffset
                return CGPoint.init(x: superPosition.x + position.x - scrollViewOffset.x , y: superPosition.y + position.y - scrollViewOffset.y)
            } else {
                let superPosition = superView.zhmfPositionInScreen()
                let position = self.frame.origin
                return CGPoint.init(x: superPosition.x + position.x, y: superPosition.y + position.y)
            }
        } else {
            return self.frame.origin
        }
    }
}
